package com.action;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.model.*;


public class afterlisting extends ActionSupport


{
	
	
	
	private String title;
	private String cond;
	//private String pic;
	private String description;
	private Float start_price;
	private Float buy_itnow_price;
	private Integer quantity;
	private String duration;
	private String listingtime;
	//private Date listingdate;
	private String payment_method;
	
	
	private String shipping_service;
	private Float shipping_cost;
	public void setShipping_cost(Float shipping_cost) {
		this.shipping_cost = shipping_cost;
	}

	
	private String free_shipping;
	private String handling_time;
	
	private File pic;
    public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public void setPic(File pic) {
		this.pic = pic;
	}

	private String filePath;

	
	public String getShipping_service() {
		return shipping_service;
	}
	public void setShipping_service(String shipping_service) {
		this.shipping_service = shipping_service;
	}
	
	public String getFree_shipping() {
		return free_shipping;
	}
	public void setFree_shipping(String free_shipping) {
		this.free_shipping = free_shipping;
	}
	public String getHandling_time() {
		return handling_time;
	}
	public void setHandling_time(String handling_time) {
		this.handling_time = handling_time;
	}

ResultSet res1,res2,res3;





	
	
	
	
	
	
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getCond() {
		return cond;
	}
	public void setCond(String cond) {
		this.cond = cond;
	}
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Float getStart_price() {
		return start_price;
	}
	public void setStart_price(Float start_price) {
		this.start_price = start_price;
	}
	public Float getBuy_itnow_price() {
		return buy_itnow_price;
	}
	public void setBuy_itnow_price(Float buy_itnow_price) {
		this.buy_itnow_price = buy_itnow_price;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public String getDuration() {
		return duration;
	}
	public void setDuration(String duration) {
		this.duration = duration;
	}
	public String getListingtime() {
		return listingtime;
	}
	public void setListingtime(String listingtime) {
		this.listingtime = listingtime;
	}
	/*public Date getListingdate() {
		return listingdate;
	}
	public void setListingdate(Date listingdate) {
		this.listingdate = listingdate;
	}*/
	public String getPayment_method() {
		return payment_method;
	}
	public void setPayment_method(String payment_method) {
		this.payment_method = payment_method;
	}
	
	
	
	
	
	
	
	public String execute()throws Exception
	{
		
		System.out.println("Injava id is");
		Map session;
		session=ActionContext.getContext().getSession();
		
		session.put("title",title);
		session.put("cond",cond);
		
		
		String sc=(String)session.get("subcatid");
		System.out.println("Injava id is"+sc);
		
		Integer id=(Integer)session.get("id");
		
		product p=new product();
		
		p.setTitle(title);
		p.setCond(cond);
		//p.setPic(pic);
		p.setDescription(description);
		p.setStart_price(start_price);
		p.setBuy_itnow_price(buy_itnow_price);
		p.setQuantity(quantity);
		p.setDuration(duration);
		p.setListingtime(listingtime);
		//p.setListingdate(listingdate);
		p.setPayment_method(payment_method);
		
		
		p.setShipping_service(shipping_service);
		p.setShipping_cost(shipping_cost);
		p.setFree_shipping(free_shipping);
		p.setHandling_time(handling_time);
		
	   	p.insert();
	   	
	   	
	   	
	   	int n=0;
	   	String q="select * from product where prodid=(select max(prodid) from product)";
	   	
	   	res1=DB.readFromBmtcDB(q);
	   	if(res1.next())
	   	{
	   			n=res1.getInt("prodid");
	   			//Integer prodid=(Integer)session.put("n");
	   			//session.put("prodid", n);
	   			System.out.println("n is: "+n);
	   	}
	   	
	   	
	   	
	   	filePath = "D:/Mtech studies/JSP project/onlinejobportal_divya/Ebay/WebContent/product_images/"+id+"_"+n+".jpeg";
 		
 		
         File newFile = new File(filePath);
         try {
 			FileUtils.copyFile(this.pic, newFile);
 		} catch (IOException e) {
 			
 			e.printStackTrace();
 		}
 		
 		session.put("prodid",n);
        System.out.println("The prodid in afterlisting is"+session.get("prodid"));
 		
 		String q1="update product set pic='"+filePath+"' where prodid="+n+";";
 		DB.update(q1);
 		p.setPic(filePath);
	    p.insert1(n);
	    
	    String insert_query="insert into listingdetail(prodid,status) values("+n+",0);";
		DB.update(insert_query);
		
		return "alist";
		
	}
	

}
